<?php

/**
 * Class PromotionController
 */
class PromotionController extends BaseController{

    /**
     * 前函数
     */
    public function init(){
        if (LoginHelper::getUserName() == '' || LoginHelper::getUserId() == 0) {
            $this->redirect($this->_createUrl('login/index'));
        }
        parent::init();
        return true;
    }

    public function addKindAction(){
        $model_advert = Controller::createMode(BaseController::$default_db,'t_26_product_promotion_kind');
        if($_POST){
            $rel = $model_advert->insert($_POST);
            if($rel){
                echo json_encode(array('code'=>200));
            }else{
                echo json_encode(array('code'=>100));
            }
        }else{
            $this -> display('add_kind');
        }
    }

    public function listAction(){
        $model_advert = Controller::createMode(BaseController::$default_db,'t_26_product_promotion');
        $pagesize = 20;
        if (!$_GET['page']) {
            $start = 0;
        } else {
            $start = ($_GET['page'] - 1) * $pagesize;
        }
        $where = '1=1';
        if($this->get('keyword')){
            $where .= " and item_no like'%".$this->get('keyword')."%'";
        }
        $base_sql = 'select *
                from t_26_product_promotion
                where '.$where;
        $base_count_sql = 'select count(1) as  data_total
                from t_26_product_promotion
                where '.$where;
        $sql = "{$base_sql} order by insert_dt desc limit {$start},{$pagesize};";

        $page = (int)$this->get('page', 1);
        $pager = $this->instance('pager');
        $pager->loadCss('classic');
        $params['w']=1;
        $page_url = $this->_createUrl('promotion/list',$params)."?page=";

        $data_total = $model_advert->execute($base_count_sql,false);

        $pager_html = $pager->setIsGoNode(false)->total($data_total['data_total'])->num($pagesize)->page($page)->url($page_url)->output();
        $this->assign('pager_html', $pager_html);
        $data  =  $model_advert->execute($sql);
        $this->assign('list',$data);
        $this -> display();
    }

    public function addAction(){
        $model_advert = Controller::createMode(BaseController::$default_db,'t_26_product_promotion');
        if($_POST){
            $rel = $model_advert->insert($_POST);
            if($rel){
                echo json_encode(array('code'=>200));
            }else{
                echo json_encode(array('code'=>100));
            }
        }else{
            $htmls = array();
            $this->assign('htmls',$htmls);
            $kinds =  $model_advert->execute("select id,title from t_26_product_promotion_kind");
            $this->assign('kinds',$kinds);
            $this -> display();
        }
    }


    public function deleteAllAction(){
        $model_advert = Controller::createMode(BaseController::$default_db,'t_26_product_promotion');
        if($model_advert->delete("1=1")){
            $this->showMessage('已经全部清除',$this->_createUrl('promotion/list'),1);
        }else{
            $this->showMessage('清空失败',$this->_createUrl('promotion/list'),1);
        }
    }

    public function kindsAction(){
        $model_advert = Controller::createMode(BaseController::$default_db,'t_26_product_promotion_kind');
        $pagesize = 20;
        if (!$_GET['page']) {
            $start = 0;
        } else {
            $start = ($_GET['page'] - 1) * $pagesize;
        }
        $where = '1=1';
        $base_sql = 'select *
                from t_26_product_promotion_kind
                where '.$where;
        $base_count_sql = 'select count(1) as  data_total
                from t_26_product_promotion_kind
                where '.$where;
        $sql = "{$base_sql} order by id desc limit {$start},{$pagesize};";

        $page = (int)$this->get('page', 1);
        $pager = $this->instance('pager');
        $pager->loadCss('classic');
        $params['w']=1;
        $page_url = $this->_createUrl('promotion/kinds',$params)."?page=";
        $data_total = $model_advert->execute($base_count_sql,false);
        $pager_html = $pager->setIsGoNode(false)->total($data_total['data_total'])->num($pagesize)->page($page)->url($page_url)->output();
        $this->assign('pager_html', $pager_html);
        $data  =  $model_advert->execute($sql);
        $this->assign('list',$data);
        $this -> display();
    }
    /*
     * 广告修改
     * */
    public function editAction(){
        $model_advert = Controller::createMode(BaseController::$default_db,'t_26_guanggaoliebiao');
        $adv_id = $this->get('id');
        if($this->post('op') =='edit'){
            $data = $this->post('data');
            $data['image'] = $this->post('upload_files');

            $rel = $model_advert->update($data,array('id'=>$this->post('id')));
            if($rel){
                echo json_encode(array('resultCode'=>200));
            }else{
                echo json_encode(array('resultCode'=>100));
            }

        }else{
            $data = $model_advert->getOne(array('id'=>$adv_id));

            $htmls['type_html'] = html::select(array('1'=>'wap','2'=>'商品','3'=>'职位','4'=>'二手淘','5'=>'供应商','6'=>'加盟商','7'=>'名店','8'=>'名厨'),array('name'=>'data[type]','class'=>'col-xs-10 col-sm-8'),$data['type']);
            $htmls['pos_html'] = html::select(array('0'=>'首页','1'=>'职位首页','2'=>'简历首页','3'=>'同城鲜配首页','4'=>'二手淘首页','5'=>'供应商首页','6'=>'加盟商','7'=>'名店名厨','8'=>'地道货'),array('name'=>'data[pos]','class'=>'col-xs-10 col-sm-8'),$data['pos']);
            $place_id = LoginHelper::getPlaceId();
            $this->assign('place_id', $place_id);
            $this->assign('adv_id', $adv_id);
            $this->assign('htmls',$htmls);
            $this->assign('data',$data);
            $this->display();
        }


    }


    /*
     * 广告删除
     * */
    public function deleteAction(){

        $model_advert = Controller::createMode(BaseController::$default_db,'t_26_product_promotion');
        $adv_id = $this->get('item_no');
        $rel = $model_advert->delete(array('item_no'=>$adv_id));
        if($rel){
            $this->showMessage('删除成功',$this->_createUrl('promotion/list'),1);
        }else{
            $this->showMessage('删除失败',$this->_createUrl('promotion/list'),1);
        }

    }

    public function deleteKindAction(){

        $model_advert = Controller::createMode(BaseController::$default_db,'t_26_product_promotion_kind');
        $model_promotion = Controller::createMode(BaseController::$default_db,'t_26_product_promotion');
        $adv_id = $this->get('id');
        $rel = $model_advert->delete(array('id'=>$adv_id));
        //删除所有促销商品
        if($rel){
            $model_promotion->delete(array('kind_id'=>$adv_id));
            $this->showMessage('删除成功',$this->_createUrl('promotion/kinds'),1);
        }else{
            $this->showMessage('删除失败',$this->_createUrl('promotion/kinds'),1);
        }

    }

    public function searchItemAction()
    {
        $model_user = Controller::createMode('db_sys_26_71', 't_26_menu');
        $model_line6 = Controller::createMode('db_line6', 't_26_menu');
        $list1 = $model_user->execute("select m_name,bar_code from t_26_menu where bar_code like '%" . $this->post('keyword') . "%'");
        $list2 = $model_line6->execute("select top 100 item_name as m_name,item_no as m_id from t_bd_item_info where item_no like  '%" . $this->post('keyword') . "%'");
        if($list2){
            $list = array_merge($list1,$list2);
        }else{
            $list = $list1;
        }
        $this->ajax(200, '成功', array('list' => $list));
    }

}
